package com.geekaca.jsp;

import com.geekaca.jsp.service.BrandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/delete")
public class DelServlet extends HttpServlet {
    private static  final Logger LOGGER = LoggerFactory.getLogger(DelServlet.class);

    private BrandService brandService = new BrandService();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("delete servlet...");
        req.setCharacterEncoding("utf-8");
        String idstr = req.getParameter("id");
        Integer id = Integer.parseInt(idstr);
        LOGGER.info("service : "+ id );

        int i = brandService.deleteBrand(id);
        if (i > 0) {
            req.getRequestDispatcher("/brands").forward(req, resp);
        }else {
            PrintWriter writer = resp.getWriter();
            writer.write("<h2>删除失败</h2>");
        }


    }
}
